﻿using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.Seedweanings
{
    public class SeedweaningSearchHnader : IRequestHandler<SeedweaningSearchCommand, ResultPage<SeedweaningQueryDto>>
    {
        private readonly IBaseRepository<Seedweaning> _seedweaningRepository;
        private readonly IBaseRepository<Cottage> _cottageRepository;
        private readonly IBaseRepository<Field> _fieldRepository;
        private readonly IMapper _mapper;

        public SeedweaningSearchHnader(IBaseRepository<Seedweaning> seedweaningRepository, IBaseRepository<Cottage> cottageRepository, IBaseRepository<Field> fieldRepository, IMapper mapper)
        {
            _seedweaningRepository = seedweaningRepository;
            _cottageRepository = cottageRepository;
            _fieldRepository = fieldRepository;
            _mapper = mapper;
        }




        /// <summary>
        /// 种母断奶显示查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<SeedweaningQueryDto>> Handle(SeedweaningSearchCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<SeedweaningQueryDto>() { Code = (int)CommonErrorCode.Search, TotalCount = 0, PagedCount = 0 };
            try
            {
                var seedweanings = await (from a in await _seedweaningRepository.GetAll()
                                          join b in await _cottageRepository.GetAll() on a.CottageId equals b.CottageId
                                          join c in await _fieldRepository.GetAll() on a.Field equals c.FieldId
                                          select new SeedweaningQueryDto()
                                          {
                                              SeedweaningId = a.SeedweaningId,
                                              Weaningdate = a.Weaningdate,
                                              Auricle = a.Auricle,
                                              Breastfeedingdays = a.Breastfeedingdays,
                                              Seedweight = a.Seedweight,
                                              Weaningnumber = a.Weaningnumber,
                                              Totallitter = a.Totallitter,
                                              CottageId = b.CottageId,
                                              Field = c.FieldId,
                                              Transferbuilding = a.Transferbuilding,
                                              Drivefield = a.Drivefield,
                                              Document = a.Document,
                                              Weaningreason = a.Weaningreason,
                                              Remark = a.Remark,
                                              CottageName = b.CottageName,
                                              FieldName = c.FieldName,
                                              CreatedOwnerId = a.CreatedOwnerId,
                                              CreatedTime = a.CreatedTime,
                                              UpdatedOwnerId = a.UpdatedOwnerId,
                                              UpdatedTime = a.UpdatedTime,
                                          }).ToListAsync();

                if (request.Weaningdate != null)
                {
                    seedweanings = seedweanings.Where(p => p.Weaningdate == request.Weaningdate).ToList();
                }
                if (!string.IsNullOrEmpty(request.Auricle))
                {
                    seedweanings = seedweanings.Where(p => p.Auricle == request.Auricle).ToList();
                }
                if (!string.IsNullOrEmpty(request.Transferbuilding))
                {
                    seedweanings = seedweanings.Where(p => p.Transferbuilding == request.Transferbuilding).ToList();
                }
                result.TotalCount = seedweanings.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                seedweanings = seedweanings.OrderByDescending(c => c.SeedweaningId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = seedweanings;
            }
            catch (Exception ex)
            {
                throw;
            }
            return result;
        }
    }
}
